package cn.hyh.core.controller;

import cn.hyh.core.component.CanTellUserException;
import cn.hyh.core.pojo.Content;
import cn.hyh.core.service.ServiceInterface.BlogService;
import cn.hyh.core.vo.BlogPageDTO;
import cn.hyh.core.vo.PageModel;
import cn.hyh.core.vo.ResultVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

/**
 * @auther :hyh
 * @desc :
 * @date :2019/7/22
 */

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    //testOOM
    @RequestMapping(value = "/aa", method = RequestMethod.GET)
    public  void  testOOM(){
        Map<Integer, Object> map = new HashMap<>();
        int i=1;
        while (true){
            map.put(++i, new HashMap<>());

        }

//        System.out.println(name);
    }
    @RequestMapping(value = "/selectPage",method = RequestMethod.POST)
    public  Map<String, Object>  selectPage(@RequestBody PageModel  pageModel){

        if ("".equals(pageModel.getType())) {
            //自定义常被全局异常捕获
            throw new CanTellUserException("文章类型不知道，无法获取数据！");
        }
        Map<String, Object> pageMap = blogService.selectPage(pageModel);

        return ResultVo.successResult(pageMap);
    }

    @RequestMapping(value = "/uploadIndexImg", method = RequestMethod.POST)
    public Map<String, Object> UploadImg(MultipartFile file, HttpServletRequest request) {
       String  url=blogService.HandleUploadImg(file, request);
          //可以
        return ResultVo.successResult(url);
    }


//    @RequestMapping(value = "/uploadEditorImg", method = RequestMethod.POST)
//    public String uploadEditorImg(MultipartFile image, HttpServletRequest request) {
//        String  url=blogService.HandleUploadImg(image, request);
//        return url;
//    }

    /**
     * 首页关键字 搜索        标题  内容搜索。
     * @param queryString
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public  Map<String,Object>  AsyncSerach(@RequestParam("keyword") String  queryString) {

        if (StringUtils.isBlank(queryString)) {
            return ResultVo.failRes("查询参数不正确!");
        }
        List<Map<String, Object>> list = blogService.AsyncSerach(queryString);
        return  ResultVo.successResult(list);

    }

    @RequestMapping(value = "/getArticle", method = RequestMethod.POST)
    public Map<String, Object> getArticle(@RequestParam("id") int id) {
        BlogPageDTO dto = blogService.getArticleDetail(id);
        return ResultVo.successResult(dto);
    }

    @RequestMapping(value = "/initCountData", method = RequestMethod.POST)
    public Map<String, Object> initCountData(@RequestParam("id") int id) {
        List<Map<String, Object>> mapList = blogService.initCountData(id);
        return ResultVo.successResult(mapList);
    }


    /**
     * 点赞。
     * @param id
     * @return
     */
    @RequestMapping(value = "/thumbUp", method = RequestMethod.POST)
    public Map<String, Object> thumbUp(@RequestParam("id") int id) {
       boolean result = blogService.thumbUp(id);
        if (result) {
            return ResultVo.successResult("点赞成功!");
        }else {
            return ResultVo.failResultMsg("点赞失败!");
        }
    }


    @RequestMapping(value = "/previousAndNext", method = RequestMethod.POST)
    public  Map<String,Object>  previousAndNext(@RequestParam("id") int id){
        Map<String, Content> map = blogService.previousAndNext(id);

        return ResultVo.successResult(map);

    }



}
